using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Reflection;
namespace trx2html
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("trx2html.exe \n  Create HTML reports of VSTS TestRuns.");
            Console.WriteLine("version:" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\n");
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: trx2html <TestResult>.trx");
                return;
            }

            string fileName = args[0];
            Transform(fileName, PrepareXsl());
            Console.WriteLine("Tranformation Succeed. OutputFile: " + fileName + ".htm\n");
        }
        private static string GetTxtBtwn(string input, string start, string end, int startIndex, bool UseLastIndexOf)
        {
            int index1 = UseLastIndexOf ? input.LastIndexOf(start, startIndex) :
                                          input.IndexOf(start, startIndex);
            if (index1 == -1) return "";
            index1 += start.Length;
            int index2 = input.IndexOf(end, index1);
            if (index2 == -1) return input.Substring(index1);
            return input.Substring(index1, index2 - index1);
        }
        private static void Transform(string fileName, XmlDocument xsl)
        {
            XslCompiledTransform x = new XslCompiledTransform();
            x.Load(xsl, new XsltSettings(true, true), null);
            string NEWFILENAME = "";
            try
            {
                string filedata = File.ReadAllText(fileName);
                string sdata = GetTxtBtwn(filedata, "xmlns=", ">", filedata.IndexOf("xmlns="), false);

                filedata = filedata.Replace("xmlns=" + sdata, "");

                string dirname = Path.GetDirectoryName(fileName);
                File.WriteAllText((dirname + "\\test1.trx"), filedata);

                NEWFILENAME = (dirname + "\\test1.trx");
            }
            catch { NEWFILENAME = fileName; }
            x.Transform(NEWFILENAME, fileName + ".htm");
            System.Diagnostics.Process.Start(fileName + ".htm");
        }

        private static XmlDocument PrepareXsl()
        {
            XmlDocument xslDoc = new XmlDocument();
            //xslDoc.Load(ResourceReader.StreamFromResource("OrcasTRX.xslt"));
            xslDoc.Load(ResourceReader.StreamFromResource("vs2012.xslt"));
            IncludeStyle(xslDoc);
            IncludeScript(xslDoc);
            return xslDoc;
        }

        private static void IncludeScript(XmlDocument xslDoc)
        {
            XmlNode scriptEl = xslDoc.GetElementsByTagName("script")[0];
            XmlAttribute scriptSrc = scriptEl.Attributes["src"];
            string script = ResourceReader.LoadTextFromResource(scriptSrc.Value);
            scriptEl.Attributes.Remove(scriptSrc);
#if !DEBUG
            script = script.Replace(";\r\n", "; ");
            script = script.Replace(",\r\n", ", ");
            script = script.Replace("}\r\n", "} ");
#endif
            scriptEl.InnerText = script;
        }

        private static void IncludeStyle(XmlDocument xslDoc)
        {
            XmlNode headNode = xslDoc.GetElementsByTagName("head")[0];
            XmlNode linkNode = xslDoc.GetElementsByTagName("link")[0];
            XmlElement styleEl = xslDoc.CreateElement("style");
            styleEl.InnerText = ResourceReader.LoadTextFromResource(linkNode.Attributes["href"].Value);
            headNode.ReplaceChild(styleEl, linkNode);
        }


    }
}
